package org.redrider.shop.domain

import grails.converters.*

class PartController {

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        def parts = Part.list( params )
        withFormat{
            html [ partInstanceList: parts, partInstanceTotal: Part.count() ]
            json {render parts as JSON}
        }

    }

    def show = {
        def partInstance = Part.get( params.id )
        withFormat{
            html {
                if(!partInstance) {
                    flash.message = "Part not found with id ${params.id}"
                    redirect(action:list)
                }
                else { return [ partInstance : partInstance ] }
            }
            json {
                if(!partInstance) {
                    render [error:"Part not found with id ${params.id}"] as JSON
                }
                else { render partInstance as deep.JSON }
            }
        }
    }

    def delete = {
        def partInstance = Part.get( params.id )
        if(partInstance) {
            try {
                partInstance.delete(flush:true)
                withFormat{
                    html{
                        flash.message = "Part ${params.id} deleted"
                        redirect(action:list)

                    }
                    json{
                        render [message:"Part ${params.id} deleted"] as JSON
                    }
                }
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                withFormat{
                    html{
                        flash.message = "Part ${params.id} could not be deleted"
                        redirect(action:show,id:params.id)
                    }
                    json{
                        render [error:"Part ${params.id} could not be deleted"] as JSON
                    }
                }
            }
        }
        else {
            withFormat{
                html{
                    flash.message = "Part not found with id ${params.id}"
                    redirect(action:list)

                }
                json{
                    render [error:"Part not found with id ${params.id}"] as JSON
                }
            }

        }
    }

    def edit = {
        def partInstance = Part.get( params.id )
        withFormat{
            html{
                if(!partInstance) {
                    flash.message = "Part not found with id ${params.id}"
                    redirect(action:list)
                }
                else {
                    return [ partInstance : partInstance ]
                }
            }
            json{
                if(!partInstance) {
                    render [error:"Part not found with id ${params.id}"] as JSON
                }
                else {
                    render partInstance as JSON
                }
            }
        }

    }

    def update = {
        def partInstance = Part.get( params.id )
        if(partInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(partInstance.version > version) {

                    partInstance.errors.rejectValue("version", "part.optimistic.locking.failure", "Another user has updated this Part while you were editing.")
                    withFormat{
                        html{
                            render(view:'edit',model:[partInstance:partInstance])
                        }
                        json{
                            render [error:"Another user has updated this Part while you were editing."] as JSON
                        }
                    }
                    return
                }
            }
            partInstance.properties = params
            if(!partInstance.hasErrors() && partInstance.save()) {
                withFormat{
                    html{
                        flash.message = "Part ${params.id} updated"
                        redirect(action:show,id:partInstance.id)

                    }
                    json {
                        render partInstance as JSON
                    }
                }
            }
            else {
                withFormat{
                    html{
                        render(view:'edit',model:[partInstance:partInstance])
                    }
                    json{
                        render [error:"Cannot update this part with id:${partInstance.id}."] as JSON
                    }
                }
            }
        }
        else {
            withFormat{
                html{
                    flash.message = "Part not found with id ${params.id}"
                    redirect(action:list)

                }
                json {render [error:"Part not found with id ${params.id}"] as JSON}
            }
        }
    }

    def create = {
        def partInstance = new Part()
        partInstance.properties = params
        withFormat{
            html ['partInstance':partInstance]
            json {render partInstance as JSON}
        }
    }

    def save = {
        def partInstance = new Part(params)
        def okStatus
        if(!partInstance.hasErrors() && partInstance.save()) {
            okStatus = true
        }
        else {
            okStatus = false
        }
        if (okStatus){
            withFormat{
                 html{
                     flash.message = "Part ${partInstance.id} created"
                     redirect(action:show,id:partInstance.id)
                 }
                json {render partInstance as JSON}
            }
        }else{
            withFormat{
                 html{
                     render(view:'create',model:[partInstance:partInstance])
                 }
                json {render [error:"Error saving Part"] as JSON}
            }
        }
    }
}
